home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / O Boy / Source / Clone_ut.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-21  |  1.7 KB  |  109 lines  |  [TEXT/R*ch]

  1. #pragma once on
  2. /*
  3.     AppAEObj_pd.h
  4.     © Bob Boylan 1996
  5.  
  6.     Revision History
  7.     MacHack 1996        initial creation
  8. */
  9.  
  10. //    -----------------------------------------------------------------
  11. //    Clone_ut ... a template class for reference counting
  12. //
  13. template <class Obj>
  14. class Clone_ut
  15. {
  16. public:
  17.     // ctor
  18.         Clone_ut( )
  19.         {
  20.             _Ref = nil;
  21.         }
  22.  
  23.         Clone_ut( Obj *inOriginal )
  24.         {
  25.             _Ref = new SRef;
  26.             if( _Ref == nil ) throw (long) memFullErr;
  27.             _Ref->_RealObj = inOriginal;
  28.             _Ref->_Count = 1;
  29.         }
  30.     
  31.     // copy ctor
  32.         Clone_ut( const Clone_ut &inOriginalClone )
  33.         {
  34.             _Ref = inOriginalClone._Ref;
  35.             IncrementRefCount( _Ref );
  36.         }
  37.  
  38.     // assignment
  39.         Clone_ut & operator =( const Clone_ut &inOtherClone )
  40.         {
  41.             if( this != &inOtherClone )
  42.             {
  43.                 SRefPtr    theRef = inOtherClone._Ref;
  44.                 IncrementRefCount( theRef );
  45.                 DecrementRefCount();
  46.                 _Ref = theRef;
  47.             }
  48.             return *this;
  49.         }
  50.  
  51.  
  52.     // dtor
  53.         ~Clone_ut()
  54.         {
  55.     
  56.             DecrementRefCount();
  57.         }
  58.     
  59.     // access to original ... sometimes you just need it
  60.         Obj & operator *()
  61.         {
  62.             if( _Ref == nil )
  63.             {
  64.                 Debugger();
  65.                 throw (long) memAdrErr;
  66.             }
  67.             return *(_Ref->_RealObj);
  68.         }
  69.  
  70.     // for checking the validity
  71.         Boolean Isnil() { return _Ref == nil; }
  72.     
  73. protected:
  74.     // the reference struct
  75.         typedef struct {    
  76.             Obj        *_RealObj;    // the real obj
  77.             Int_32    _Count;        // and the number of references
  78.         } SRef, *SRefPtr;
  79.  
  80.     // data    
  81.         SRefPtr    _Ref;
  82.     
  83.     
  84.     // DecrementRefCount - a worker
  85.         void DecrementRefCount()
  86.         {
  87.             if( _Ref != nil )
  88.             {
  89.                 --_Ref->_Count;
  90.                 if( _Ref->_Count == 0 )
  91.                 {
  92.                     delete _Ref->_RealObj;
  93.                     _Ref->_RealObj = nil;
  94.                     delete _Ref;
  95.                     _Ref = nil;
  96.                 }
  97.             }
  98.         }
  99.     
  100.     // IncrementRefCount - a worker
  101.         void IncrementRefCount( SRefPtr &ioRef )
  102.         {
  103.             ++ioRef->_Count;
  104.         }
  105.     
  106.     
  107. private:
  108. };
  109.